package orbitPrototype.gui;

import orbitPrototype.Util;
import orbitPrototype.loaders.XMLLoader;

/**
 * Description:	Handles gui styles. For now, just holds the style name and standard measurements required
 * 				by the code. Also checks whether all resources required by the style is present.
 * @author Drapan
 *
 */
public class GUIStyle
{
	private String _styleName = "";
	
	private int _windowHeaderHeight = 0;
	private int _windowHeaderSideWidth = 0;
	private int _windowFrameDimension = 0;
	
	private boolean _windowTiled = false;
	private int _windowTileSize = 0;
	
	private int _buttonFrameDimension = 0;
	
	private static final String[] REQUIRED_RESOURCES_BUTTON = {	"button_corner", "button_edge_h", "button_edge_v", 
																"button_fill", "button_corner_hover", "button_edge_h_hover", 
																"button_edge_v_hover", "button_fill_hover"};
	
	private static final String[] REQUIRED_RESOURCES_WINDOW = {	"window_corner", "window_edge_h", "window_edge_v", "window_fill",
																"window_header", "window_header_side_l", "window_header_side_r"};
	
	private static final String[][] REQUIRED_RESOURCES = {REQUIRED_RESOURCES_BUTTON, REQUIRED_RESOURCES_WINDOW};

	
	private static final String[] REQUIRED_SQUARE = {"button_corner", "window_corner"};
	
	public GUIStyle(String styleName)
	{
		_styleName = styleName;
		
		validateAndGetDimensions();
	}
	
	private void validateAndGetDimensions()
	{
		for(int i = 0; i < REQUIRED_RESOURCES.length; i++)
		{
			for(int j = 0; j < REQUIRED_RESOURCES[i].length; j++)
			{
				if(!XMLLoader.getXMLElementByUniqueNameExists("spriteLookup_gui", _styleName + "_" + REQUIRED_RESOURCES[i][j]))
				{
					Util.error("Cannot find resource " + REQUIRED_RESOURCES[i][j] + " in gui style " + _styleName + ".");
					continue;
				}
				
				if(Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_" + REQUIRED_RESOURCES[i][j]))) % 2 != 0 
						|| Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_" + REQUIRED_RESOURCES[i][j]))) % 2 != 0 )
				{
					Util.error("Resource " + REQUIRED_RESOURCES[i][j] + " in gui style " + _styleName + " requires even numbers for width/height.");
				}
			}
		}
		
		for(int i = 0; i < REQUIRED_SQUARE.length; i++)
		{
			if(Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_" + REQUIRED_SQUARE[i]))) / 
					Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_" + REQUIRED_SQUARE[i]))) != 1 )
			{
				Util.error("Resource " + REQUIRED_SQUARE[i] + " in gui style " + _styleName + " requires to be square.");
			}
		}
		
		_windowHeaderHeight = (Integer.parseInt(XMLLoader.getElementValue("height", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_window_header"))));
		_windowHeaderSideWidth = (Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_window_header_side_l"))));
		_windowFrameDimension = (Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_window_corner"))));
		
		_buttonFrameDimension = (Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_window_corner"))));
		
		if(XMLLoader.getXMLElementByUniqueNameExists("spriteLookup_gui", _styleName + "_window_fill_tile"))
		{
			_windowTileSize = (Integer.parseInt(XMLLoader.getElementValue("width", XMLLoader.getXMLElementByUniqueName("spriteLookup_gui", _styleName + "_window_fill_tile"))));
		}
	}

	public String getName()
	{
		return _styleName;
	}

	public int getWindowHeaderHeight()
	{
		return _windowHeaderHeight;
	}

	public int getWindowHeaderSideWidth()
	{
		return _windowHeaderSideWidth;
	}

	public int getWindowFrameDimension()
	{
		return _windowFrameDimension;
	}

	public int getButtonFrameDimension()
	{
		return _buttonFrameDimension;
	}
}
